package com.feihong.txgw.tx_precenter;

import android.text.TextUtils;
import android.util.Log;

import com.feihong.txgw.inter.contract.home.IHomeContract;
import com.feihong.txgw.json.HomeJSON;
import com.feihong.txgw.message.RefreshMsgIconMessage;
import com.feihong.txgw.model.bean.home.HomeDialogShowModel;
import com.feihong.txgw.model.bean.user.CityInfo;
import com.feihong.txgw.tx_model.HomeMainModel;
import com.feihong.txgw.utils.UserLoginInfo;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName HomeMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/3 11:42
 * @Describe
 * @Change
 */
public class HomeMainPrecenter extends IHomeContract.AbHomePrecenter {

    public static HomeMainPrecenter create(){
        return new HomeMainPrecenter();
    }
    String locationText = null;
    @Override
    public void loadHomeData() {
        mIModel.getHomeData().subscribe(new Observer<HomeJSON>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(HomeJSON homeJSON) {
                mIView.setHomeData(homeJSON);

            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
                mIView.loadDataComplated();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void loadHomeRecommendMsg() {
        mIModel.getHomeRecommendMsg().subscribe(new Observer<HomeDialogShowModel>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(HomeDialogShowModel result) {
                mIView.showHomeRecommendMsgDialog(result);
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
                mIView.loadDataComplated();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void updateLocatoin() {
        locationText = null;
        CityInfo cityInfo = UserLoginInfo.getUserCityInfo();
        if (cityInfo != null && !TextUtils.isEmpty(cityInfo.getCityName())) {
            locationText = cityInfo.getCityName();
        }
        if (!TextUtils.isEmpty(locationText)) {
            String str;
            if (locationText.length() > 4) {
                str = locationText.substring(0, 4) + "..";
            } else {
                str = locationText;
            }
            mIView.updateLocatoin(str);
        }
    }

    @Override
    public void loadHomeUnreadMsgCount() {
        mIModel.getHomeUnreadMsg().subscribe(new Observer<RefreshMsgIconMessage>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(RefreshMsgIconMessage result) {
                mIView.setHomeUnReadMsgStatus(result);
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
                mIView.loadDataComplated();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void loadHomeAllData() {
        mIModel.getHomeAllDate().subscribe(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(Object o) {

                if(o instanceof RefreshMsgIconMessage){
                    RefreshMsgIconMessage refreshMsgIconMessage = (RefreshMsgIconMessage) o;
                    mIView.setHomeUnReadMsgStatus(refreshMsgIconMessage);
                }else if(o instanceof HomeDialogShowModel){
                    HomeDialogShowModel homeDialogShowModel = (HomeDialogShowModel) o;
                    mIView.showHomeRecommendMsgDialog(homeDialogShowModel);
                }else if(o instanceof HomeJSON){
                    HomeJSON homeJSON = (HomeJSON) o;
                    mIView.setHomeData(homeJSON);
                }else {
                    Log.i("通讯顾问", "onNext: 解析错误"+o.toString());
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
                mIView.loadDataComplated();
            }

            @Override
            public void onComplete() {

                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public IHomeContract.IHomeMainModel getModel() {
        return HomeMainModel.create();
    }

    @Override
    public void onStart() {

    }
}
